package com.ukao.steward.ui.valetRunners.storeCreatOrder;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.SearchUserBean;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.ImageUtils;
import com.ukao.steward.util.MyDateUtils;
import com.ukao.steward.widget.ShapedImageView;
import com.ukao.steward.widget.StateButton;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 门店客户信息
 */
public class StoreClientInfoFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.create_order_btn)
    Button createOrderBtn;
    Unbinder unbinder;
    @BindView(R.id.search_tv)
    TextView searchTv;
    @BindView(R.id.headimgPath)
    ShapedImageView headimgPath;
    @BindView(R.id.product_name)
    TextView name;
    @BindView(R.id.recharge_btn)
    StateButton rechargeBtn;
    @BindView(R.id.balance)
    TextView balance;
    @BindView(R.id.point)
    TextView point;
    @BindView(R.id.couponCnt)
    TextView couponCnt;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.verifyPhone)
    ImageView verifyPhone;
    @BindView(R.id.cardNo)
    TextView cardNo;
    @BindView(R.id.isBinding)
    ImageView isBinding;
    @BindView(R.id.name1)
    TextView name1;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.birthday)
    TextView birthday;
    @BindView(R.id.age)
    TextView age;
    @BindView(R.id.cardName)
    TextView cardName;
    @BindView(R.id.user_group_name)
    TextView userGroupName;
    @BindView(R.id.user_info_part6_tag1)
    TextView userInfoPart6Tag1;
    @BindView(R.id.user_info_part6_tag2)
    TextView userInfoPart6Tag2;
    @BindView(R.id.user_info_part6_tag3)
    TextView userInfoPart6Tag3;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.cardDiscountName)
    TextView cardDiscountName;
    private String timeType = "yyyy年 MM月 dd日";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private SearchUserBean mSearchUserBean;


    public static StoreClientInfoFragment newInstance(SearchUserBean muserInfoCard, String param2) {
        StoreClientInfoFragment fragment = new StoreClientInfoFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, muserInfoCard);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSearchUserBean = getArguments().getParcelable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_store_client_info, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }
    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        setStatusBarTintColor(R.color.colorPrimary);


    }

    @Override
    protected void initView() {
        if (!CheckUtils.isNull(mSearchUserBean)) {

            if (mSearchUserBean.getWxId()==1){
                Drawable drawable= getResources().getDrawable(R.drawable.client_info_wechat);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                name.setCompoundDrawables(null, null, drawable, null);
            }
            sex.setText(mSearchUserBean.getSex());
            cardName.setText( mSearchUserBean.getRfidNo());
            ImageUtils.loadHeadImage(_mActivity,mSearchUserBean.getHeadimgPath(),headimgPath);
            name.setText(CheckUtils.isEmptyString(mSearchUserBean.getName()));
            cardName.setText(CheckUtils.isEmptyString(mSearchUserBean.getCardName()));
            point.setText(mSearchUserBean.getPoint() + "积分");
            couponCnt.setText(mSearchUserBean.getCouponCnt()+"张");
            balance.setText(CheckUtils.isEmptyNumber(mSearchUserBean.getBalance()));
            phone.setText(CheckUtils.isEmptyString(mSearchUserBean.getPhone()));
            name1.setText(CheckUtils.isEmptyString(mSearchUserBean.getName()));
            SearchUserBean.userGroupBean userGroup = mSearchUserBean.getUserGroup();
            userGroupName.setText(CheckUtils.isNull(userGroup) ? "" : userGroup.getName());
            List<SearchUserBean.AddressListBean> addressList = mSearchUserBean.getAddressList();
            address.setText(CheckUtils.isEmpty(addressList) ? "" : addressList.get(0).getRegionalDesc()+addressList.get(0).getDesc());
            verifyPhone.setImageResource(mSearchUserBean.getVerifyPhone() == 1 ? R.drawable.client_info_check : R.drawable.client_info_no_check);
            isBinding.setImageResource(mSearchUserBean.getIsBinding() ? R.drawable.client_info_bing : R.drawable.client_info_no_bing);
            cardDiscountName.setText(mSearchUserBean.getCardDiscountName());
            try {
                if (!CheckUtils.isEmpty(mSearchUserBean.getBirthday())) {
                    Date dataBirthday = MyDateUtils.stringDate(mSearchUserBean.getBirthday(), "yyyy-MM-dd");
                    birthday.setText(MyDateUtils.getTime(dataBirthday, timeType));  /*生日*/
                    age.setText(MyDateUtils.getAge(dataBirthday) + "岁");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            List<SearchUserBean.TagListBean> mTagData = mSearchUserBean.getTagList();
            if (!CheckUtils.isEmpty(mTagData)) {

                for (int i = 0; i < mTagData.size(); i++) {
                    String mTagName = mTagData.get(i).getName();
                    if (CheckUtils.isEmpty(mTagName)) return;
                    switch (i) {
                        case 0:
                            userInfoPart6Tag1.setText(mTagName);
                            userInfoPart6Tag1.setVisibility(View.VISIBLE);
                            break;
                        case 1:
                            userInfoPart6Tag2.setText(mTagName);
                            userInfoPart6Tag2.setVisibility(View.VISIBLE);
                            break;
                        case 2:
                            userInfoPart6Tag3.setText(mTagName);
                            userInfoPart6Tag3.setVisibility(View.VISIBLE);
                            break;
                    }
                }

            }

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.create_order_btn, R.id.recharge_btn,R.id.search_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.create_order_btn:
                    start(OrderSubmitFragment.newInstance(mSearchUserBean,""));

                break;
            case R.id.recharge_btn:
                break;
            case R.id.search_tv :
                finish();
                break;
        }
    }
}
